/**
 * 
 * Copyright (C) 2011 Cody Stoutenburg . All rights reserved.
 *
 *       This program is free software; you can redistribute it and/or
 *       modify it under the terms of the GNU Lesser General Public License
 *       as published by the Free Software Foundation; either version 2.1
 *       of the License, or (at your option) any later version.
 *
 *       This program is distributed in the hope that it will be useful,
 *       but WITHOUT ANY WARRANTY; without even the implied warranty of
 *       MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *       GNU Lesser General Public License for more details.
 *
 *       You should have received a copy of the GNU Lesser General Public License
 *       along with this program; if not, write to the Free Software
 *       Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA. 
 * 
 */
package ca.usherbrooke.builder.feature;

import ca.usherbrooke.application.MyTiledMap;
import ca.usherbrooke.model.world.WorldModel;

/**
 * @author Cody Stoutenburg
 * 
 */
public class WorldModelBuilder {
	private static final String TILE_PROPERTY_BLOCK = "block";
	private static final String TILE_PROPERTY_GROUND = "ground";

	/**
	 * build the world ground and wall from a tiledMap
	 * 
	 * @param tmap
	 * @return the world build
	 */
	public static WorldModel generateWorld(MyTiledMap tmap) {
		WorldModel model = new WorldModel(tmap.getTileWidth(), 0, 0,
				tmap.getWidth() * tmap.getTileWidth(), tmap.getHeight()
						* tmap.getTileHeight());

		for (int layer = 0; layer < tmap.getLayerCount(); layer++) {
			for (int x = 0; x < tmap.getWidth(); x++) {
				for (int y = 0; y < tmap.getHeight(); y++) {
					int tid = tmap.getTileId(x, y, layer);

					if (tid != 0) {
						boolean isBlocking = Boolean.valueOf(tmap
								.getTileProperty(tid, TILE_PROPERTY_BLOCK,
										"false"));

						boolean isGround = Boolean.valueOf(tmap
								.getTileProperty(tid, TILE_PROPERTY_GROUND,
										"false"));

						if (isGround) {
							model.addGround(x, y, GroundModelBuilder
									.generateGround(tmap, tid));
						}
						if (isBlocking) {
							model.addWall(x, y,
									WallModelBuilder.generateWall(tmap, tid));
						}
					}
				}
			}
		}

		return model;
	}
}
